package com.github.cyf.rateLimit.controller;

import com.github.cyf.common.module.Rs;
import com.github.cyf.rateLimit.annotation.CounterRateLimit;
import com.github.cyf.rateLimit.annotation.LeakyBucketRateLimit;
import com.github.cyf.rateLimit.annotation.SlidingWindowRateLimit;
import com.github.cyf.rateLimit.annotation.TokenBucketRateLimit;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenyifan
 * @create 2025-01-13 22:37
 */
@RestController
@RequestMapping("/rateLimit")
@Api(tags = "限流模块")
public class RateLimitController {

    @ApiOperation(value = "计时器（限流）")
    @ApiOperationSupport(order = 1)
    @GetMapping("/counter")
    @CounterRateLimit(requests = 5, timeWindow = 3)
    public Rs  counter() {
        return Rs.ok("counter test ok!");
    }

    @ApiOperation(value = "滑动窗口（限流）")
    @ApiOperationSupport(order = 2)
    @GetMapping("/slidingWindow")
    @SlidingWindowRateLimit(requests = 3, timeWindow = 3)
    public Rs slidingWindow() {
        return Rs.ok("slidingWindow test ok!");
    }

    @ApiOperation(value = "令牌桶（限流）")
    @ApiOperationSupport(order = 3)
    @GetMapping("/tokenBucket")
    @TokenBucketRateLimit(permitsPerSecond = 1)
    public Rs tokenBucket() {
        return Rs.ok("tokenBucket test ok!");
    }

    @ApiOperation(value = "漏桶（限流）")
    @ApiOperationSupport(order = 4)
    @GetMapping("/leakyBucket")
    @LeakyBucketRateLimit(capacity = 3, leakRate = 1)
    public Rs leakyBucket() {
        return Rs.ok("leakyBucket test ok!");
    }
}